package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.ScanDetailVo;
import java.sql.SQLException;
import java.util.List;

/**
 * @author gyy
 * @date 2017/9/12
 */

public class ScanDetailVoDao extends BaseDao<ScanDetailVo, Long> {

  public ScanDetailVoDao() {
    super(DbHelper.getInstance(BaseApplication.getContext()), ScanDetailVo.class);
  }

  /**
   * 根据操作类型，员工，配载单获取数据
   *
   * @param offlineTaskNo 离线任务号
   * @param operTypeCode 操作类型
   * @param userCode 员工
   */
  public List<ScanDetailVo> getDataByStowageAndType(String offlineTaskNo, int operTypeCode, String userCode) {
    try {
      QueryBuilder<ScanDetailVo, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("offlineTaskNo", offlineTaskNo);
      where.and().eq("operTypeCode", operTypeCode);
      where.and().eq("operEmpCode", userCode);
      List<ScanDetailVo> listUnloadScanTable = queryBuilder.orderBy("scanTime", false).orderBy("id", false).query();
      return listUnloadScanTable;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 根据配载单删除
   */
  public int deleteByTaskNo(String stowageNo) {
    int result = -1;
    try {
      DeleteBuilder<ScanDetailVo, Long> deleteBuilder = dao.deleteBuilder();
      deleteBuilder.where().eq("stowageNo", stowageNo);
      result = deleteBuilder.delete();
    } catch (Exception e) {
      e.printStackTrace();
      result = -1;
    }
    return result;
  }
}
